//
//  XMPhotoBrowserAnimator.swift
//  sinaDemo
//
//  Created by Xueming on 2021/4/1.
//

import UIKit

//@objc protocol XMPhotoBrowserDelegate:NSObjectProtocol {
//
//   @objc optional func getImageViewOriginalFrame() -> CGRect
//   @objc optional func getImageViewFinalFrame() -> CGRect
//   @objc optional func getImage() -> UIImage
//}
class XMPhotoBrowserAnimator:
    NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {
    var isPresened :Bool = false
    
    var originalFrame:CGRect = CGRect.zero
    var finalFrame:CGRect = CGRect.zero

    lazy var tempImageView:UIImageView = UIImageView().then {
        $0.contentMode = .scaleAspectFill
        $0.clipsToBounds = true
    }
    /// 弹出和消失动画的时间
    /// - Parameter transitionContext: 上下文
    /// - Returns: 秒
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    /// 转场上下文，可以获取弹出和消失的view
    ///UITransitionContextFromViewKey, and UITransitionContextToViewKey
    /// - Parameter transitionContext: 上下文
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        //获取弹出的view
        if self.isPresened {
            guard let presenedView = transitionContext.view(forKey: UITransitionContextViewKey.to) else { return  }
            transitionContext.containerView.addSubview(presenedView)
            //添加动画imageview 到 transitionContext.containerView
            transitionContext.containerView.addSubview(tempImageView)
            tempImageView.frame = originalFrame
            let x:CGFloat = 0.0
            var y:CGFloat = 0.0
            let width:CGFloat = kScreenWidth
            let height:CGFloat = kScreenWidth / (tempImageView.image?.size.width ?? 1) * (tempImageView.image?.size.height ?? 1)
            if height > kScreenHeight {
                y = 0
            }else{
                y = CGFloat(kScreenHeight * 0.5 - height * 0.5)
            }
            finalFrame = CGRect(x: x, y: y, width: width, height: height)
            presenedView.alpha = 0
            transitionContext.containerView.backgroundColor = .black
            transitionContext.containerView.alpha = 0
            UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
                self.tempImageView.frame = self.finalFrame
                transitionContext.containerView.alpha = 1
            } completion: {
                presenedView.alpha = 1
                if $0 {
                    self.tempImageView.removeFromSuperview()
                    transitionContext.completeTransition(true)
                }
            }
        }else {
            guard let presenedView = transitionContext.view(forKey: UITransitionContextViewKey.from) else { return  }
            transitionContext.containerView.addSubview(presenedView)
            //添加动画
            //添加动画imageview 到 transitionContext.containerView
            transitionContext.containerView.addSubview(tempImageView)
            let x:CGFloat = 0.0
            var y:CGFloat = 0.0
            let width:CGFloat = kScreenWidth
            let height:CGFloat = kScreenWidth / (tempImageView.image?.size.width ?? 1) * (tempImageView.image?.size.height ?? 1)
            if height > kScreenHeight {
                y = 0
            }else{
                y = CGFloat(kScreenHeight * 0.5 - height * 0.5)
            }
            finalFrame = CGRect(x: x, y: y, width: width, height: height)
            presenedView.alpha = 0
            tempImageView.frame = finalFrame
            transitionContext.containerView.backgroundColor = .black
            transitionContext.containerView.alpha = 1
            UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
                transitionContext.containerView.alpha = 0
                self.tempImageView.frame = self.originalFrame
            } completion: {
                presenedView.alpha = 1
                if $0 {
                    self.tempImageView.removeFromSuperview()
                    transitionContext.completeTransition(true)
                }
            }
        }
       
    }
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.isPresened = true
        return self
    }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.isPresened = false
        return self
    }
}
